package moddbbackendrevisited.resource;

import java.sql.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.*;
import moddbbackendrevisited.Main;
import moddbbackendrevisited.schema.*;

@Path("/tables/{id}")
public class TableResource {

  /*
   */
   
    @GET
    @Produces("application/xml")
    public Response get(@PathParam("id") String id) throws SQLException {

        ResultSet rs = Main.dbCon.getMetaData().getTables(null, null, id, new String[] {"TABLE"});

        try {
            if (rs.next()) {
                return Response.ok(new Table(id)).build();
            } else {
                return Response.status(Status.NOT_FOUND).build();
            }
        } finally {
            rs.close();
        }
    }

    @DELETE
    public Response delete(@PathParam("id") String tableName) {

        Statement stmnt;
        try {
            stmnt = Main.dbCon.createStatement();
        } catch (SQLException e) {
            return Response.status(Status.SERVICE_UNAVAILABLE).build();
        }

        try {
            stmnt.executeUpdate("DROP TABLE " + tableName);
            return Response.status(Status.NO_CONTENT).build();
        } catch (SQLException e) {
            e.printStackTrace();
            return Response.status(Status.NOT_FOUND).build();
        }
    }
}
